package org.yiqixue.accounts.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Positive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.yiqixue.accounts.constants.TransactionConstants;
import org.yiqixue.accounts.dto.DepositRequest;
import org.yiqixue.accounts.dto.ErrorResponseDto;
import org.yiqixue.accounts.dto.ResponseDto;
import org.yiqixue.accounts.dto.TransactionDto;
import org.yiqixue.accounts.service.ITransactionService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Tag(
        name = "交易操作的REST APIs",
        description = "用于创建和查询账户交易的REST APIs"
)
@RestController
@RequestMapping(path="/api/transactions", produces = {MediaType.APPLICATION_JSON_VALUE})
@Validated
public class TransactionController {

    private final ITransactionService transactionService;

    @Autowired
    public TransactionController(ITransactionService transactionService) {
        this.transactionService = transactionService;
    }

    @Operation(
            summary = "创建存款交易REST API",
            description = "为指定手机号对应的客户账户创建一笔存款交易"
    )
    @ApiResponses({
            @ApiResponse(
                    responseCode = "201",
                    description = "交易创建成功"
            ),
            @ApiResponse(
                    responseCode = "500",
                    description = "内部服务器错误",
                    content = @Content(
                            schema = @Schema(implementation = ErrorResponseDto.class)
                    )
            )
    })
    @PostMapping("/deposit")
    public ResponseEntity<ResponseDto> createDeposit(@Valid @RequestBody DepositRequest request) {
        
        transactionService.createDeposit(
                request.getMobileNumber(), 
                request.getAmount(), 
                request.getDescription() != null ? request.getDescription() : "存款"
        );
        
        return ResponseEntity
                .status(HttpStatus.CREATED)
                .body(new ResponseDto(TransactionConstants.STATUS_201, TransactionConstants.MESSAGE_201));
    }

    @Operation(
            summary = "查询账户交易历史REST API",
            description = "根据账户号查询交易历史"
    )
    @ApiResponses({
            @ApiResponse(
                    responseCode = "200",
                    description = "成功获取交易历史"
            ),
            @ApiResponse(
                    responseCode = "500",
                    description = "内部服务器错误",
                    content = @Content(
                            schema = @Schema(implementation = ErrorResponseDto.class)
                    )
            )
    })
    @GetMapping("/{accountNumber}")
    public ResponseEntity<List<TransactionDto>> getTransactions(@PathVariable Long accountNumber) {
        List<TransactionDto> transactions = transactionService.getTransactionsByAccountNumber(accountNumber);
        return ResponseEntity.ok(transactions);
    }

    @Operation(
            summary = "按日期范围查询账户交易历史REST API",
            description = "根据账户号和日期范围查询交易历史"
    )
    @ApiResponses({
            @ApiResponse(
                    responseCode = "200",
                    description = "成功获取交易历史"
            ),
            @ApiResponse(
                    responseCode = "500",
                    description = "内部服务器错误",
                    content = @Content(
                            schema = @Schema(implementation = ErrorResponseDto.class)
                    )
            )
    })
    @GetMapping("/{accountNumber}/date-range")
    public ResponseEntity<List<TransactionDto>> getTransactionsByDateRange(
            @PathVariable Long accountNumber,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
        
        List<TransactionDto> transactions = transactionService.getTransactionsByAccountNumberAndDateRange(
                accountNumber, startDate, endDate);
        
        return ResponseEntity.ok(transactions);
    }
} 